import {baseUrl} from '@/prod'
import {UniAppCssProcessorOptions} from "@dcloudio/uni-cli-shared/dist/postcss/plugins/uniapp";

let addressURL = baseUrl.baseApi;
const http = (url = '', type: any = 'GET', data = {}, address = 1, header = {}) => {
  return new Promise((resolve, reject) => {
    if (address == 1) {
      addressURL = baseUrl.baseApi;
    } else if (address == 2) {
      addressURL = baseUrl.baseApi2;
    }
    uni.request({
      header,
      method: type,
      url: addressURL + url,
      data: data,
      dataType: 'json',
      sslVerify: false,
    })
      .then((response: UniAppCssProcessorOptions | any) => {
        if (response.statusCode === 200) {
          if (response.data && response.data.status === 0) {
            resolve(response.data);
          } else if(response.data.status===1000){
            resolve(response.data);
            uni.showToast({
              icon: 'none',
              title: response.data.msg
            })
          }else {
            uni.showToast({
              icon: 'none',
              title: response.data.msg
            })
            reject(false)
          }
        } else if (response.statusCode === 404) {
          uni.showToast({
            icon: 'none',
            title: '请求地址不存在'
          })
          reject(false)
        } else {
          reject(false);
        }
      }).catch(error => {
      console.log(error)
      reject(error)
    })

  })
}
export default http
